package InterviewPractise.regExp;

import java.awt.print.Book;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: Richard
 * Date: 5/20/11
 * Time: 9:05 AM
 * To change this template use File | Settings | File Templates.
 */
public class StringIps {
    private static String exp = "((\\d{1,3})(\\.)){3}(\\d{1,3})";
    private static String string = "192.168.1.3";
    public static void main(String[] args){
        StringIps ips = new StringIps();
        if(ips.isMatch(exp,string)){
           System.out.println(ips.getIp(exp,string));
        }
    }

    public boolean isMatch(String exp , String ip){
        Pattern pattern = Pattern.compile(exp);
        Matcher matcher = pattern.matcher(ip);
        if(matcher.find()&&(ip.length()==getIp(exp,ip).length())){
            return true;
        }
        else return false;
    }


    public String getIp(String exp , String ip){
        Pattern pattern = Pattern.compile(exp);
        Matcher matcher = pattern.matcher(ip);
        if(matcher.find()){
           return  matcher.group();
        }
        return "";
    }
}
